#!/bin/sh
#######################################################################################################计算内存容量
TOTAL_FREE=$(free | grep Mem |awk '{print $2}')
EUREKA_FREE=`expr ${TOTAL_FREE} / 64 / 1024`
CONFIG_FREE=`expr ${TOTAL_FREE} / 64 / 1024`
AUTH_FREE=`expr ${TOTAL_FREE} / 8 / 1024`
BASIC_FREE=`expr ${TOTAL_FREE} / 8 / 1024`
BIZ_FREE=`expr ${TOTAL_FREE} / 3 / 1024`
FRONT_FREE=`expr ${TOTAL_FREE} / 8 / 1024`
#######################################################################################################jar包名称变量,需要根据实际jar包名称新增或修改
export EUREKA=sinog2c-eureka.jar
export CONFIG=sinog2c-config.jar
export AUTH=sinog2c-auth.jar
export BASIC=sinog2c-basic.jar
export BIZ=sinog2c-biz.jar
export FRONT=sinog2c-front.jar
export AUTH192=sinog2c-auth192.jar
export FRONT192=sinog2c-front192.jar
#######################################################################################################把上面jar包名称变量加入数组，需要手工加入
export services=($EUREKA $CONFIG $AUTH $BASIC $BIZ $FRONT $AUTH192 $FRONT192)
export services1=(eureka config auth basic biz front auth192 front192)
#######################################################################################################java位置，如果设置了环境变量直接写java即可，有一些服务器shell读取不到jdk环境变量，就可以写java的绝对路径
export java_location='java -jar'
#######################################################################################################服务端口变量，根据实际情况修改
export eureka_port=8765
export config_port=12000
export auth_port=8888
export basic_port=8201
export biz_port=8001
export front_port=8090
export auth192_port=8889
export front192_port=8091
#######################################################################################################服务环境分支变量，根据实际情况修改
export env=-Dspring.profiles.active=prod
#######################################################################################################服务启动jvm参数变量，根据实际情况修改
export EUREKA_JVM=' -Xmx'${EUREKA_FREE}'m -Xms'${EUREKA_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_eureka.log'
export CONFIG_JVM=' -Xmx'${CONFIG_FREE}'m -Xms'${CONFIG_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_config.log'
export AUTH_JVM=' -Xmx'${AUTH_FREE}'m -Xms'${AUTH_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_auth.log
-Ddb_url=192.158.97.4:3307/azbj-province-bj
-Ddb_username=root
-Ddb_password=052E63988CC6414F4A9E47EEAEC6BEFB
-DclientWebPage=http://172.26.73.245:8090
-Dsso_type=4
-Dsso_singeKey=ah0yug8tytmGBq4dfMhQpA==
-Ddownloadfilepath_unix=/azbj/uploadfile/fileTemplate/
-Dsecurity_insert_password=$2a$10$tvkWLVUVK05xZej1snadxu/9Q804ZHgSWPmrUR7x61vyCbGtUUwn6
-Dcaptcha_state=false
-Dcaptcha_locktime=30
-Dcaptcha_locknum=5
-Dcaptcha_singlelocktime=5
-DEUREKA_HOST=localhost
-DAPP_NAME=sinog2c-auth
-Dlocal_code=110000'
export BASIC_JVM=' -Xmx'${BASIC_FREE}'m -Xms'${BASIC_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_basic.log
-Ddb_url=192.158.97.4:3307/azbj-province-bj
-Ddb_username=root
-Ddb_password=052E63988CC6414F4A9E47EEAEC6BEFB
-Dsecurity_reset_password=$2a$10$tvkWLVUVK05xZej1snadxu/9Q804ZHgSWPmrUR7x61vyCbGtUUwn6'
export BIZ_JVM=' -Xmx'${BIZ_FREE}'m -Xms'${BIZ_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_biz.log
-Ddb_url=192.158.97.4:3307/azbj-province-bj
-Ddb_username=root
-Ddb_password=052E63988CC6414F4A9E47EEAEC6BEFB
-Dmiddle_db_url=59.255.21.17:3309
-Dshadow_db_url=192.168.1.130/azbj-clean-shadow
-Dshadow_db_username=root
-Dshadow_db_password=7F7F332279864C66
-Ddm_db_url=10.211.55.4:5236/azbjMiddle
-Ddm_db_username=SYSDBA
-Ddm_db_password=111111111
-Doracle_db_url=jdbc:oracle:thin:@192.168.1.60:1522:ORACL
-Doracle_db_username=azbj
-Doracle_db_password=azbj
-Dbuji_db_url=59.255.21.17:3309
-Dzijian_db_url=59.255.21.17:3309
-Dlocal_code=110000
-Dif_open_idnum_tm=2
-Dmongodb_host=59.255.21.17
-Dmongodb_port=27027
-Dredis_host=192.158.97.4
-Dredis_port=6380
-Dredis_password=Gkzx!2020=bj
-Dburedis_host=59.255.21.17
-Dburedis_port=6380
-Dftp_ip=59.255.21.17
-Dftp_port=2121
-Dftp_datapath=/azbj/ftp/beijing/
-Duploadfilepath_unix=/azbj/uploadfile/
-Dshadow_mysql_open=false
-Dshadow_dm_open=false
-Dshadow_oracle_open=false
-Dshadow_hljMysql_open=false
-Dshadow_gdOracle_open=false
-Dhn_gxh_open=false'
export FRONT_JVM=' -Xmx'${FRONT_FREE}'m -Xms'${FRONT_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_front.log
-Dssl_enabled=false
-Dredis_host=192.158.97.4
-Dredis_port=6380
-Dredis_password=Gkzx!2020=bj
-Dauth_server_url=http://172.26.73.245:8888/auth
-Dauth_server_url_diy=http://192.158.97.2:8888/auth
-Dlogout_url=http://172.26.73.245:8888/auth/logout
-Dsolr_mh_yaz=http://192.158.97.3:8988/solr/mh_yaz
-Dsolr_mh_azbj=http://192.158.97.3:8988/solr/mh_azbj
-Dsolr_mh_jcab=http://192.158.97.3:8988/solr/mh_jcab
-Dsolr_gj_yaz=http://192.158.97.3:8988/solr/gj_yaz
-Dsolr_gj_azbj=http://192.158.97.3:8988/solr/gj_azbj
-Dsolr_gj_jcab=http://192.158.97.3:8988/solr/gj_jcab
-Duploadfilepath_unix=/opt/azbj/uploadfile/
-DfileTemplate_unix=/opt/azbj/uploadfile/fileTemplate/
-Dsecurity_user_default_password=Azbj@123456
-Dsecurity_open_qz_change_password=false
-Dlogin_use_state=false
-DuploadFile_fileSuffix=.rar,.zip,.doc,.docx,.xls,.xlsx,.pdf,.jpg,.bmp,.jpeg,.png
-Dsso_enabled=false
-DEUREKA_HOST=localhost
-DAPP_NAME=sinog2c-front
-Dlocal_code=110000'
export AUTH_HLW_JVM=' -Xmx'${AUTH_FREE}'m -Xms'${AUTH_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_auth.log
-Ddb_url=192.158.97.4:3307/azbj-province-bj
-Ddb_username=root
-Ddb_password=052E63988CC6414F4A9E47EEAEC6BEFB
-DclientWebPage=http://192.158.97.2:8091
-Dsso_type=4
-Dsso_singeKey=ah0yug8tytmGBq4dfMhQpA==
-Ddownloadfilepath_unix=/azbj/uploadfile/fileTemplate/
-Dsecurity_insert_password=$2a$10$tvkWLVUVK05xZej1snadxu/9Q804ZHgSWPmrUR7x61vyCbGtUUwn6
-Dcaptcha_state=false
-Dcaptcha_locktime=30
-Dcaptcha_locknum=5
-Dcaptcha_singlelocktime=5
-DEUREKA_HOST=localhost
-DAPP_NAME=sinog2c-auth192
-Dlocal_code=110000'
export FRONT_HLW_JVM=' -Xmx'${FRONT_FREE}'m -Xms'${FRONT_FREE}'m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc_log/gc_log_front.log
-Dssl_enabled=false
-Dredis_host=192.158.97.4
-Dredis_port=6380
-Dredis_password=Gkzx!2020=bj
-Dauth_server_url=http://192.158.97.2:8889/auth
-Dauth_server_url_diy=http://192.158.97.2:8889/auth
-Dlogout_url=http://192.158.97.2:8889/auth/logout
-Dsolr_mh_yaz=http://192.158.97.3:8988/solr/mh_yaz
-Dsolr_mh_azbj=http://192.158.97.3:8988/solr/mh_azbj
-Dsolr_mh_jcab=http://192.158.97.3:8988/solr/mh_jcab
-Dsolr_gj_yaz=http://192.158.97.3:8988/solr/gj_yaz
-Dsolr_gj_azbj=http://192.158.97.3:8988/solr/gj_azbj
-Dsolr_gj_jcab=http://192.158.97.3:8988/solr/gj_jcab
-Duploadfilepath_unix=/opt/azbj/uploadfile/
-DfileTemplate_unix=/opt/azbj/uploadfile/fileTemplate/
-Dsecurity_user_default_password=Azbj@123456
-Dsecurity_open_qz_change_password=false
-Dlogin_use_state=false
-DuploadFile_fileSuffix=.rar,.zip,.doc,.docx,.xls,.xlsx,.pdf,.jpg,.bmp,.jpeg,.png
-Dsso_enabled=false
-DEUREKA_HOST=localhost
-DAPP_NAME=sinog2c-front192
-Dlocal_code=110000'
######################################################################################################启动参数整合
export LEVEL_eureka_PARAM=$java_location' -Dserver.port='$eureka_port' '$env'  '$EUREKA_JVM
export LEVEL_config_PARAM=$java_location' -Dserver.port='$config_port'         '$CONFIG_JVM
export LEVEL_auth_PARAM=$java_location'   -Dserver.port='$auth_port'   '$env'  '$AUTH_JVM
export LEVEL_basic_PARAM=$java_location'  -Dserver.port='$basic_port'  '$env'  '$BASIC_JVM
export LEVEL_biz_PARAM=$java_location'    -Dserver.port='$biz_port'    '$env'  '$BIZ_JVM
export LEVEL_front_PARAM=$java_location'  -Dserver.port='$front_port'  '$env'  '$FRONT_JVM
export LEVEL_auth192_PARAM=$java_location'   -Dserver.port='$auth192_port'   '$env'  '$AUTH_HLW_JVM
export LEVEL_front192_PARAM=$java_location'  -Dserver.port='$front192_port'  '$env'  '$FRONT_HLW_JVM
######################################################################################################交互引导开始
flag=0
while [ $flag -eq 0 ]
do
	mark=''
	for ((ratio=0;${ratio}<=100;ratio+=5))
	do
        sleep 0.05
        printf "正在加载脚本:[%-40s]%d%%\r" "${mark}" "${ratio}"
        mark="##${mark}"
	done
	echo
	echo "=========================================================================="
	echo "==========================安置帮教自动shell脚本工具=========================="
	echo "=========================================================================="
cat <<EOF
44.[选择启动服务]
55.[批量启动/重启服务]
66.[选择停止服务]
77.[批量停止服务]
88.[查看进程]
99.[退出]
EOF

read -p "请输入序号安装服务，输入99退出:" num
expr $num +1 &> /dev/null
if [ "$?" -eq 0 ];then
echo "input int"
  exit 1
elif [ "$num" -eq 0 ];then
  echo "input error"
  exit 1
fi

#44.[选择启动服务]
[ $num -eq 44 ] && {
	for((i=1; i<=${#services[*]}; i++))
	do
	    echo $i".[install ${services[$i-1]}]"
	done
	read -p "请输入序号安装服务，输入99退出:" num
	expr $num +1 &> /dev/null
	if [ "$?" -eq 0 ];then
	echo "input int"
	  exit 1
	elif [ "$num" -eq 0 ];then
	  echo "input error"
	  exit 1
	fi
	####################################################################################################判断服务
	for((i=1; i<=${#services1[*]}; i++))
	do
	    if [ $num -eq $i ];
		then
			temp=LEVEL_${services1[$i-1]}_PARAM
			temp1=$(eval echo \$${temp})
			temp_param=$temp1

			temp2=${services1[$i-1]}_port
			temp3=$(eval echo \$${temp2})
			temp_port=$temp3
			temp_name=sinog2c-${services1[$i-1]}.jar
		fi
	done
	##############################################################################################启动服务
	[ $num -eq 1 -o $num -eq 2 -o $num -eq 3 -o $num -eq 4 -o $num -eq 5 -o $num -eq 6  ] && {
		echo "-------- 开始安装 $temp_name 服务 --------"
		if [ ! -d "./gc_log/" ];then
			mkdir ./gc_log
		fi
		if [ ! -d "./service_log/" ];then
			mkdir ./service_log
		fi
		nohup  $temp_param  $temp_name >./service_log/$temp_name.log 2>&1 &
		pid=`lsof -i:$temp_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$pid" ]
		      do
		         pid=`lsof -i:$temp_port|grep "LISTEN"|awk '{print $2}'`
		      done
		echo "-------- pid is $pid --------"
		echo "-------- $temp_name 启动成功 --------"
	}
}

#55.[批量启动/重启服务]
[ $num -eq 55 ] && {
	for((i=1; i<=${#services1[*]}; i++))
	do
		temp=LEVEL_${services1[$i-1]}_PARAM
		temp1=$(eval echo \$${temp})
		temp_param=$temp1

		temp2=${services1[$i-1]}_port
		temp3=$(eval echo \$${temp2})
		temp_port=$temp3
		temp_name=sinog2c-${services1[$i-1]}.jar

		#如果当前服务已经启动，则先kill掉
		pid_now=`lsof -i:$temp_port|grep "LISTEN"|awk '{print $2}'`
		[ -n "$pid_now" ] && {
			echo "--------  $temp_name 服务正在运行，正在停止 --------"
			sleep 1s
			kill -9 $pid_now
			echo "--------  $temp_name 服务已停止!准备重新启动! --------"
		}

		echo "-------- 开始安装 $temp_name 服务 --------"
		if [ ! -d "./gc_log/" ];then
			mkdir ./gc_log
		fi
		if [ ! -d "./service_log/" ];then
			mkdir ./service_log
		fi
		nohup  $temp_param  $temp_name >./service_log/$temp_name.log 2>&1 &
		pid=`lsof -i:$temp_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$pid" ]
		      do
		         pid=`lsof -i:$temp_port|grep "LISTEN"|awk '{print $2}'`
		      done
		mark1=''
		for ((ratio=0;${ratio}<=100;ratio+=5))
		do
	        sleep 0.2
	        printf $temp_name"正在启动:[%-40s]%d%%\r" "${mark1}" "${ratio}"
	        mark1="##${mark1}"
		done
		echo
		echo "-------- pid is $pid --------"
		echo "-------- $temp_name 启动成功 --------"
	done
}

#66.[选择停止服务]
[ $num -eq 66 ] && {
	for((i=7; i<=${#services[*]}+6; i++))
	do
	    echo $i".[停止服务 ${services[$i-7]}]"
	done

	read -p "请输入序号停止服务，输入99退出:" num
	expr $num +1 &> /dev/null
	if [ "$?" -eq 0 ];then
	echo "input int"
	  exit 1
	elif [ "$num" -eq 0 ];then
	  echo "input error"
	  exit 1
	fi
	[ $num -eq 7 ] && {
		pid=`lsof -i:$eureka_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- eureka已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- eureka服务未启动，无须停止 --------"
		}
	}
	[ $num -eq 8 ] && {
		pid=`lsof -i:$config_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- config已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- config服务未启动，无须停止 --------"
		}
	}
	[ $num -eq 9 ] && {
		pid=`lsof -i:$auth_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- auth已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- auth服务未启动，无须停止 --------"
		}
	}
	[ $num -eq 10 ] && {
		pid=`lsof -i:$basic_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- basic已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- basic服务未启动，无须停止 --------"
		}
	}
	[ $num -eq 11 ] && {
		pid=`lsof -i:$biz_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- biz已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- biz服务未启动，无须停止 --------"
		}
	}
	[ $num -eq 12 ] && {
		pid=`lsof -i:$front_port|grep "LISTEN"|awk '{print $2}'`
		flag2=0
		[ -n "$pid" ] && {
			sleep 1s
			kill -9 $pid
			flag2=1
			echo "-------- front已停止 --------"
		}
		[ $flag2 -eq 0 ] && {
			echo "-------- front服务未启动，无须停止 --------"
		}
	}
}

#77.[批量停止服务]
[ $num -eq 77 ] && {
	pid1=`lsof -i:$eureka_port|grep "LISTEN"|awk '{print $2}'`
	pid2=`lsof -i:$config_port|grep "LISTEN"|awk '{print $2}'`
	pid3=`lsof -i:$auth_port|grep "LISTEN"|awk '{print $2}'`
	pid4=`lsof -i:$basic_port|grep "LISTEN"|awk '{print $2}'`
	pid5=`lsof -i:$biz_port|grep "LISTEN"|awk '{print $2}'`
	pid6=`lsof -i:$front_port|grep "LISTEN"|awk '{print $2}'`
	flag1=0
	[ -n "$pid1" ] && {
		sleep 1s
		kill -9 $pid1
		flag1=1
		echo "-------- eureka已停止 --------"
	}
	[ -n "$pid2" ] && {
		sleep 1s
		kill -9 $pid2
		flag1=1
		echo "-------- config已停止 --------"
	}
	[ -n "$pid3" ] && {
		sleep 1s
		kill -9 $pid3
		flag1=1
		echo "-------- auth已停止 --------"
	}
	[ -n "$pid4" ] && {
		sleep 1s
		kill -9 $pid4
		flag1=1
		echo "-------- basic已停止 --------"
	}
	[ -n "$pid5" ] && {
		sleep 1s
		kill -9 $pid5
		flag1=1
		echo "-------- biz已停止 --------"
	}
	[ -n "$pid6" ] && {
		sleep 1s
		kill -9 $pid6
		flag1=1
		echo "-------- front已停止 --------"
	}
	[ $flag1 -eq 0 ] && {
		echo "-------- 当前没有服务运行,无须停止 --------"
	}
}

#88.[查看进程]
[ $num -eq 88 ] && {
	pid1=`lsof -i:$eureka_port|grep "LISTEN"|awk '{print $2}'`
	pid2=`lsof -i:$config_port|grep "LISTEN"|awk '{print $2}'`
	pid3=`lsof -i:$auth_port|grep "LISTEN"|awk '{print $2}'`
	pid4=`lsof -i:$basic_port|grep "LISTEN"|awk '{print $2}'`
	pid5=`lsof -i:$biz_port|grep "LISTEN"|awk '{print $2}'`
	pid6=`lsof -i:$front_port|grep "LISTEN"|awk '{print $2}'`
	flag1=0
	[ -n "$pid1" ] && {
		echo "-------- eureka启动成功，端口号:$eureka_port,pid:$pid1 --------"
		flag1=1
	}
	[ -n "$pid2" ] && {
		echo "-------- config启动成功，端口号:$config_port,pid:$pid2 --------"
		flag1=1
	}
	[ -n "$pid3" ] && {
		echo "-------- auth启动成功，端口号:$auth_port,pid:$pid3 --------"
		flag1=1
	}
	[ -n "$pid4" ] && {
		echo "-------- basic启动成功，端口号:$basic_port,pid:$pid4 --------"
		flag1=1
	}
	[ -n "$pid5" ] && {
		echo "-------- biz启动成功，端口号:$biz_port,pid:$pid5 --------"
		flag1=1
	}
	[ -n "$pid6" ] && {
		echo "-------- front启动成功，端口号:$front_port,pid:$pid6 --------"
		flag1=1
	}
	[ $flag1 -eq 0 ] && {
		echo "-------- 当前没有服务运行 --------"
	}
}

#99.[退出]
[ $num -eq 99 ] && {
	flag=1
	exit 1
}
done
#################################################################################################